//
//  MobileBindViewController.m
//  XPhoto
//
//  Created by sentinel on 2020/5/14.
//  Copyright © 2020 baboy. All rights reserved.
//

#import "MobileBindViewController.h"
#import <BKKit/UIViewController+x.h>
#import "VerificationCodeViewController.h"
#import "User.h"

@interface MobileBindViewController ()<VerificationCodeDelegate, UITextFieldDelegate>
@end

@implementation MobileBindViewController
- (id)init{
    if (self = [super initWithNibName:NSStringFromClass([self class]) bundle:nil]){
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.mobileTextField.text = self.mobile;
    
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"image.png"]
                                                                             style:UIBarButtonItemStylePlain
                                                                            target:self
                                                                            action:@selector(cancel:)];
}
- (void)cancel:(id)button{
    [self dismissViewControllerAnimated:YES completion:nil];
}
- (IBAction)nextStep:(id)button{
    BOOL ret = checkInput([self checkConfig], ^(NSString *errorMsg) {
        [BKIndicator showMessageAndFadeOut:errorMsg completion:nil];
    });
    if (!ret) {
        return;
    }
    NSString *mobile = [self.mobileTextField.text stringByReplacingOccurrencesOfString:@" " withString:@""];
    [self sendSmsCode:mobile callback:^(id  _Nullable response, NSError * _Nullable error) {
        if (error) {
            return;
        }
        VerificationCodeViewController *vc = [[VerificationCodeViewController alloc] init];
        vc.mobile = mobile;
        vc.delegate = self;
        [self.navigationController pushViewController:vc animated:YES];
    }];
}
- (void)sendSmsCode:(NSString *)mobile callback:(nullable void (^)( id _Nullable response,  NSError * _Nullable error))callback{
    [BKIndicator showMessage:NSLocalizedString(@"sending", @"提示")];
    [User sendBindMobileVCode:mobile callback:^(id  _Nonnull task, id  _Nullable response, NSError * _Nullable error) {
        
        if (error) {
            [BKIndicator showMessageAndFadeOut:error.localizedDescription completion:nil];
        }
        if(!error){
            [BKIndicator showMessageAndFadeOut:NSLocalizedString(@"vcode_send_success", @"提示") completion:nil];
        }
        if (callback) {
            callback(response, error);
        }
    }];
}
- (void)bindWithMobile:(NSString *)mobile vcode:(NSString *)vcode{
    [BKIndicator showMessage:NSLocalizedString(@"binding", @"提示")];
    [User bindMobile:mobile
               vcode:vcode
            callback:^(id  _Nonnull task, id  _Nullable json, NSError * _Nullable error) {
        if (error) {
            [BKIndicator showMessageAndFadeOut:error.localizedDescription completion:nil];
            return;
        }
        [BKIndicator showMessage:NSLocalizedString(@"bind_success", @"提示")];
        if (self.delegate) {
            [self.delegate mobileBindViewController:self didBindWithMobile:mobile];
        }
        
    }];
}
#pragma mark - UITextViewDelegate
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
    NSString *mobile = textField.text;
    self.submitButton.hidden = [self.mobile isEqualToString:mobile];
    return YES;
}
#pragma mark - VerificationCodeDelegate
- (void)verificationCodeViewControllerWillResendVCode:(VerificationCodeViewController *)vc{
    [self sendSmsCode:vc.mobile callback:nil];
}
- (void)verificationCodeViewController:(VerificationCodeViewController *)vc didInputVCode:(NSString *)vcode{
    [self bindWithMobile:vc.mobile vcode:vcode];
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
